package com.vortex.cloud.sample.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-13 周三
 * @time 17:27
 * @description
 */
@Getter
public enum RoleType {

    ADMIN("0", "管理员"), STUDENT("1", "普通用户");


    private final String code;
    private final String desc;

    RoleType(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static RoleType getByCode(String code) {
        if (StringUtils.isBlank(code)) {
            throw new RuntimeException("枚举参数错误");
        }
        RoleType[] values = values();
        Optional<RoleType> found = Arrays.stream(values).filter(it -> it.code.equals(code)).findAny();
        return found.isPresent() ? found.get() : null;
    }
}
